package ATM_mysql;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 转账界面
 */
public class TransferPanel extends JPanel implements ActionListener {
    private ATM frame;
    private JLabel otherAccountLabel = new JLabel("请输入对方账号");
    private JLabel balanceLabel = new JLabel("请输入转账金额");
    private JTextField otherAccountTextField = new JTextField();
    private JTextField balanceTextField = new JTextField();
    private JButton sureBtn = new JButton(Util.getIcon("/images/buttonSure.jpg"));
    private JButton backBtn = new JButton(Util.getIcon("/images/buttonOff.jpg"));
    private Font font = new Font("宋体",Font.BOLD,25);
    public TransferPanel(ATM frame){
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        otherAccountLabel.setBounds(300,200,200,50);
        otherAccountLabel.setFont(font);
        this.add(otherAccountLabel);

        otherAccountTextField.setBounds(500,200,200,50);
        otherAccountTextField.setFont(font);
        this.add(otherAccountTextField);

        balanceLabel.setBounds(300,250,200,50);
        balanceLabel.setFont(font);
        this.add(balanceLabel);

        balanceTextField.setBounds(500,250,200,50);
        balanceTextField.setFont(font);
        this.add(balanceTextField);

        sureBtn.setBounds(350,350,140,50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        backBtn.setBounds(520,350,140,50);
        backBtn.addActionListener(this);
        this.add(backBtn);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == sureBtn){
            // 获取输入的对方账号
            String otherAccount = otherAccountTextField.getText();
            double balance = 0;
            try {
                // 获取输入的转账金额
                balance = Double.parseDouble(balanceTextField.getText());
            }catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(this,"转账金额必须为数字，并不能为空");
                return;
            }

            // 当期登录的账户
            User loginUser = frame.getLoginUser();
            DBUtil db = new DBUtil();
            User otherUser = db.queryByName(otherAccount);
            // 判断账号是否存在
            if(otherUser==null){
                JOptionPane.showMessageDialog(this,"查无此人");
                return;
            }
            // 判断是否转给自己
            if(loginUser.getAccount().equals(otherAccount)){
                JOptionPane.showMessageDialog(this,"不能转给自己");
                return;
            }
            // 判断余额是否足够
            if(balance > loginUser.getBalance()){
                JOptionPane.showMessageDialog(this,"余额不足");
                return;
            }
            // 当前账户扣钱
            loginUser.setBalance(loginUser.getBalance() - balance);
            // 对方账户加钱
            otherUser.setBalance(otherUser.getBalance() + balance);
            //更新数据库
            db.update(loginUser,otherUser);
            JOptionPane.showMessageDialog(this,"转账成功");
            frame.switchPanel("menu");
        }else if(e.getSource() == backBtn){
            frame.switchPanel("menu");

        }
    }
}
